package com.chinemit.cui.components.navigation.tab
{
	
	import com.chinemit.cui.components.core.CUIComponent;
	import com.chinemit.cui.events.SelectEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	[Event(name="selectChange", type="com.chinemit.cui.events.SelectEvent")]
	public class TabBar extends CUIComponent
	{
		public static const LAYOUT_VERTICAL:String ="vertical";
		public static const LAYOUT_HORIZONTAL:String ="horizontal";
		private var _currentIndex:int;
		private var _indexSum:int;
		private var itemRenderClass:Class;
		
		private var _gap:int = 5;
		
		private var _layout:String = "horizontal";

		public function get layout():String
		{
			return _layout;
		}

		public function set layout(value:String):void
		{
			_layout = value;
			init(indexSum,currentIndex)
		}

		public function TabBar(_itemRenderer:Class)
		{
			super();
			
			if(_itemRenderer == null){
				return;
			}
			itemRenderClass = _itemRenderer;
		}
		
		/**
		 * Tab Item Gap 默认间隔为5 
		 * @return 
		 * 
		 */		
		public function get gap():int
		{
			return _gap;
		}
		
		public function set gap(value:int):void
		{
			_gap = value;
		}
		
		
		public function get indexSum():int
		{
			return _indexSum;
		}

		public function set indexSum(value:int):void
		{
			_indexSum = value;
		}

		public function get currentIndex():int
		{
			return _currentIndex;
		}

		public function set currentIndex(value:int):void
		{
			_currentIndex = value;
			reset();
			//发送SELECT事件
			this.dispatchEvent(new SelectEvent(SelectEvent.EVENT_SELECT,false,false,currentIndex));
		}
		
		/**
		 * 设置 
		 * @param sum
		 * @param index
		 * @param _data
		 * 
		 */		
		public function init(sum:int,index:int):void{
			_currentIndex = index;
			indexSum = sum;
			
			if(!itemRenderClass){
				return;
			}
			var btn:*;
//			while(this.numChildren!=0){
//				btn = this.getChildAt(i) as IndexBarButton;
//				btn.removeEventListener(MouseEvent.CLICK,selectHandler);
//				this.removeChild(btn);
//				btn = null;
//			}
			this.removeAllChild();
			var i:int= 0;
			if(this.layout == LAYOUT_HORIZONTAL){
				for(i=0;i<indexSum;i++){
					btn = new itemRenderClass();
					this.addChild(btn);
					btn.x = i*(btn.width+gap);
					
					if(i==currentIndex){
						btn.setFocus();
					}
					btn.index = i;
					btn.addEventListener(MouseEvent.CLICK,selectHandler);
				}
			}else{
				for(i=0;i<indexSum;i++){
					btn = new itemRenderClass();
					this.addChild(btn);
					btn.y = i*(btn.height+gap);
					
					if(i==currentIndex){
						btn.setFocus();
					}
					btn.index = i;
					btn.addEventListener(MouseEvent.CLICK,selectHandler);
				}
			}
			this.dispatchEvent(new SelectEvent(SelectEvent.EVENT_SIZE_CHANGE));
		}
		
		/**
		 * 重新设置 
		 * 
		 */		
		private function reset():void{
			
			var btn:*;
			for(var i:int= 0 ;i<indexSum;i++){
				
				if(this.getChildAt(i) is itemRenderClass){
					btn = this.getChildAt(i) as itemRenderClass;
					btn.reset();
				}
				
				if(i==currentIndex){
					btn.setFocus();
				}
			 
			}
			
		}
		
		private function selectHandler(event:MouseEvent):void{
			
			var btn:TabBarItem; 
			btn = event.target as TabBarItem;
			
			if(btn.index == this.currentIndex){
				return;
			}
			
			for(var i:int= 0 ;i<indexSum;i++){
				btn = this.getChildAt(i) as TabBarItem;
				btn.reset();
			}
			btn = event.target as TabBarItem;
			btn.setFocus();
			currentIndex = btn.index;
		}
	}
}